package su.jet.irc.client.service;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.UnknownHostException;

import su.jet.common.irc.domain.User;
import su.jet.common.irc.protocol.Session;

public class ClientService {

	private Session connectionWithServer;
	private Session connectionWithReader;
	
	public void connect() {
		ConnectionService connectionService =  new ConnectionService();
		try {
			connectionWithServer = connectionService.getConnectionWithServer();
		} catch (UnknownHostException e) {
			System.out.println("Connection with server is impossible!");
			//e.printStackTrace();
		} catch (IOException e) {
			System.out.println("Connection with server failed!");
			//e.printStackTrace();
		}
		try {
			connectionWithReader = connectionService.getConnectionWithReader();
		} catch (UnknownHostException e) {
			System.out.println("Connection with reader console is impossible!");
			//e.printStackTrace();
		} catch (IOException e) {
			System.out.println("Run reader console at first!");
			//e.printStackTrace();
		}
	}
	
	public MessageHandler getMessageHandler(User user) {
		return new MessageHandler(user);
	}
	
	public Authenticator getAuthenticator() {
		BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
		Authenticator authenticator =  new Authenticator(connectionWithServer, connectionWithReader, input);
		return authenticator;
	}
	
	public WriteService getWriteService(MessageHandler messageHandler) {
		return new WriteService(connectionWithServer, connectionWithReader, messageHandler);
	}
	
	public ReceiveService getReceiveService(MessageHandler messageHandler) {
		return new ReceiveService(connectionWithServer, connectionWithReader, messageHandler);
	}	
}
